import 'dart:io';

import 'package:bigegg/page/guide/xmm_guide_page.dart';
import 'package:bigegg/page/login/xmm_login_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'common/sp/xmm_shared_preferences_util.dart';
import 'global/routes/xmm_routes.dart';
import 'global/xmm_storage_manager.dart';
import 'page/login/xmm_login_binding.dart';
import 'utils/xmm_platform_utils.dart';

void main() async {
  /// 确保初始化
  WidgetsFlutterBinding.ensureInitialized();

  /// 配置初始化
  await XmmSPUtil.init();
  await XmmStorageManager.init();

  /// 预初始化
  await XmmPlatformUtils.preInit();
  await XmmPlatformUtils.init();

  /// app入口
  runApp(const MyApp());

  /// Android状态栏透明
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.dark,
      statusBarIconBrightness: Brightness.dark,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  ///loading style init
  configLoading();
}

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = false
    ..dismissOnTap = false
    ..maskType = EasyLoadingMaskType.clear;
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(primaryColor: Colors.white),
          getPages: XmmRoutes.routePage,
          builder: EasyLoading.init(),
          initialBinding: XmmLoginBinding(),
          home: getAppHomeWidget(),
        );
      },
    );
  }

  Widget getAppHomeWidget() {
    if (XmmSPUtil.get("isFirstOpenApp", defValue: true)) {
      return const XmmGuidePage();
    } else {
      return const XmmLoginPage();
    }
  }
}
